package com.photoeditor.demo.ui.widget.image.hair;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.common.base.ui.widget.image.RoundImageView;
import com.photoeditor.R;
import com.photoeditor.demo.model.image.collage.templet.BgBean;
import com.photoeditor.demo.model.image.collage.templet.ColorBgBean;
import com.photoeditor.demo.model.image.collage.templet.InsideResBgBean;

import java.util.ArrayList;

public class HairColorListAdapter extends ArrayAdapter<BgBean> {

    private ArrayList<BgBean> mResId;

    private LayoutInflater mInfl;

    private int mSelectedPosition = 0;

    private final int mItemWidth;

    private final int mItemHeight;

    public HairColorListAdapter(Context context, ArrayList<BgBean> data) {
        super(context, 0, data);
        mResId = data;
        mInfl = ((Activity) context).getLayoutInflater();
        mItemWidth = context.getResources().getDimensionPixelSize(R.dimen.edit_beauty_hair_item_width);
        mItemHeight = context.getResources().getDimensionPixelSize(R.dimen.edit_beauty_hair_item_height);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInfl.inflate(R.layout.item_hair_color, null);
            RoundImageView imageView = (RoundImageView) convertView.findViewById(R.id.color_view);
            AdapterView.LayoutParams lp = new AdapterView.LayoutParams(mItemWidth, mItemHeight);
            convertView.setLayoutParams(lp);
            View selectView = convertView.findViewById(R.id.hair_select_bg);
            holder = new ViewHolder();
            holder.backgroundView = imageView;
            holder.selectView = selectView;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        BgBean bean = mResId.get(position);
        if (bean instanceof ColorBgBean) {
            ColorBgBean colorBgBean = (ColorBgBean) bean;
            holder.backgroundView.setImageDrawable(colorBgBean.get());
        } else if (bean instanceof InsideResBgBean) {
            InsideResBgBean insideResBgBean = (InsideResBgBean) bean;
            holder.backgroundView.setImageResource(insideResBgBean.get());
        } else if (bean instanceof InsideResColorBean) {
            InsideResColorBean insideResBgBean = (InsideResColorBean) bean;
            holder.backgroundView.setImageResource(insideResBgBean.get());
        }
        if (mSelectedPosition == position) {
            holder.selectView.setVisibility(View.VISIBLE);
        } else {
            holder.selectView.setVisibility(View.GONE);
        }

        return convertView;
    }

    @Override
    public int getCount() {
        if (mResId != null) {
            return mResId.size();
        }
        return 0;
    }

    @Override
    public BgBean getItem(int position) {
        if (mResId != null) {
            return mResId.get(position);
        }
        return null;
    }

    public void setData(ArrayList<BgBean> data) {
        mResId = data;
        notifyDataSetChanged();
    }

    /**
     * @param position
     * @param convertView
     */
    public void setSelectedPosition(int position, View convertView) {
        mSelectedPosition = position;
        notifyDataSetChanged();
    }

    public void setSelectedPosition(int position) {
        mSelectedPosition = position;
        notifyDataSetChanged();
    }

    public int getSelectedPosition() {
        return mSelectedPosition;
    }

    public class ViewHolder {
        RoundImageView backgroundView;
        View selectView;
    }
}
